package com.shellyan.realtime.gmallpublisher.controller;

import com.alibaba.fastjson.JSON;
import com.shellyan.realtime.gmallpublisher.bean.Option;
import com.shellyan.realtime.gmallpublisher.bean.SaleInfo;
import com.shellyan.realtime.gmallpublisher.bean.Stat;
import com.shellyan.realtime.gmallpublisher.service.PublisherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Shelly An
 * @create 2020/9/4 15:53
 */
@RestController
public class PublisherController {
    /**
     * 根据实现类创建对象 从服务层获取数据
     */
    @Autowired
    PublisherService service;

    /**
     * 根据需求 http://localhost:8070/realtime-total?date=2020-09-04 说明是get请求
     *
     * @param date 2020-09-04
     * @return json字符串
     */
    @GetMapping("/realtime-total")
    public String realtimeTotal(String date) {
        Long dau = service.getDau(date);

        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("id", "dau");
        map1.put("name", "新增日活");
        map1.put("value", dau);

        result.add(map1);

        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("id", "new_mid");
        map2.put("name", "新增设备");
        map2.put("value", 233);

        result.add(map2);

        Map<String, Object> map3 = new HashMap<String, Object>();
        map2.put("id", "order_amount");
        map2.put("name", "新增交易额");
        map2.put("value", service.getTotalAmount(date));

        result.add(map3);

        return JSON.toJSONString(result);

    }

    /**
     * map里超过10个kv，不要用map返回--可读性差，但灵活，用bean返回。
     * http://localhost:8070/realtime-hour?id=dau&date=2020-09-04
     * http://localhost:8070/realtime-hour?id=order_amount&date=2020-09-04
     *
     * @param id
     * @param date
     * @return
     */
    @GetMapping("/realtime-hour")
    public String realtimeHours(String id, String date) {
        if ("dau".equals(id)) {
            Map<String, Long> today = service.getHourDau(date);
            Map<String, Long> yesterday = service.getHourDau(getYesterday(date));

            Map<String, Map<String, Long>> result = new HashMap<>();
            result.put("yesterday", yesterday);
            result.put("today", today);

            return JSON.toJSONString(result);
        } else if ("order_amount".equals(id)) {
            Map<String, BigDecimal> today = service.getHourTotalAmount(date);
            Map<String, BigDecimal> yesterday = service.getHourTotalAmount(getYesterday(date));

            Map<String, Map<String, BigDecimal>> result = new HashMap<>();
            result.put("yesterday", yesterday);
            result.put("today", today);

            return JSON.toJSONString(result);
        }
        return "ok";
    }

    @GetMapping("/sale_detail")
    public String saleDetail(String date, int startpage, int size, String keyword) throws IOException {
        Map<String, Object> saleDetailAndAggs = service.getSaleDetailAndAggs(date, keyword, startpage, size);
        /*System.out.println(saleDetailAndAggs);*/
        SaleInfo saleInfo = new SaleInfo();

        Long total = (Long) saleDetailAndAggs.get("total");
        saleInfo.setTotal(total);

        List<Map> detail = (List<Map>) saleDetailAndAggs.get("detail");
        saleInfo.setDetails(detail);

        Map<String, Long> genderAgg = (Map<String, Long>) saleDetailAndAggs.get("genderAgg");
        Stat genderStat = new Stat();

        saleInfo.addStat(genderStat);

        genderStat.setTitle("用户性别占比");
        for (Map.Entry<String, Long> entry : genderAgg.entrySet()) {
            Option opt = new Option();
            opt.setName(entry.getKey().equals("F") ? "女" : "男");
            opt.setValue(entry.getValue());
            genderStat.addOption(opt);
        }


        Map<String, Long> ageAgg = (Map<String, Long>) saleDetailAndAggs.get("ageAgg");
        Stat ageStat = new Stat();

        saleInfo.addStat(ageStat);

        genderStat.setTitle("用户年龄占比");

        ageStat.addOption(new Option("20岁以下", 0l));
        ageStat.addOption(new Option("20岁到30岁", 0l));
        ageStat.addOption(new Option("30岁及30岁以上", 0l));

        for (String key : ageAgg.keySet()) {
            int age = Integer.parseInt(key);
            Long num = ageAgg.get(key);
            if (age < 20) {
                Option stat = ageStat.getOptions().get(0);
                stat.setValue(stat.getValue() + num);
            } else if (age < 30) {
                Option stat = ageStat.getOptions().get(1);
                stat.setValue(stat.getValue() + num);
            } else {
                Option stat = ageStat.getOptions().get(2);
                stat.setValue(stat.getValue() + num);
            }
        }


        return JSON.toJSONString(saleInfo);
    }


    /**
     * 返回昨天的日期
     *
     * @param date
     * @return
     */
    private String getYesterday(String date) {
        return LocalDate.parse(date).plusDays(-1).toString();
    }
}
